<?php
/**
 * @ClassName: Merchant
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-09-15 09:58
 * @Version: V1.0.0
 */

namespace app\api\controller\merchant;

use app\api\controller\Api;
use app\common\enum\CommonEnum;
use app\common\enum\MerchantEnum;
use app\common\model;

class Merchant extends Api
{
    /**
     * 获取商户入驻保证金
     * @return \think\response\Json
     */
    function getEarnestMoney()
    {
        $webConfig = web_config('merchant_config');
        return json(['code' => 1, 'msg' => '获取成功', 'data' => $webConfig]);
    }

    /**
     * 商户信息
     * @return \think\response\Json
     */
    function info()
    {
        $merchant = model\Merchant::where('id', $this->user_id)->find();

        if (!$merchant) {
            return json(['code' => 0, 'msg' => '商户不存在']);
        }

        // 获取商户下首个门店
        $store = model\Store::where('merchant_id', $merchant['id'])->where('is_first_one', 1)->find();

        $result = [
            'id' => $merchant['id'],
            'merchant_id' => $merchant['id'],
            'store_id' => $store['id'] ?? 0,
            'audit_opinion' => $merchant['audit_opinion'],
            'checked' => false,
            // 基础信息
            'base_info' => [
                'merchant_type' => $merchant['merchant_type'],//商户类型，1小微，2个体，3企业
                'merchant_short_name' => $merchant['merchant_short_name'],//商户简称
                'user_type' => $merchant['user_type'],//用户类型，1联盟商家，2水站商家
                'contact_name' => $merchant['contact_name'],//商户联系人姓名
                'contact_phone' => $merchant['contact_phone'],//商户联系电话
                'email' => $merchant['email'],//邮箱
                'store_name' => $merchant['store_name'],////店铺名称
                'category_id' => $merchant['category_id'],//商户分类
                'unity_category_id' => $merchant['unity_category_id'],//行业类目ID
                'remark' => $merchant['remark'],//备注
            ],
            // 经营者信息
            'legal_person' => [
                'legal_name' => $merchant['legal_name'],//经营者姓名
                'legal_num' => $merchant['legal_num'],//经营者身份证号
                'legal_address' => $merchant['legal_address'],//经营者身份证上的居住地
                'legal_idcard_front_photo' => $merchant['legal_idcard_front_photo'],//经营者身份证正面
                'legal_idcard_back_photo' => $merchant['legal_idcard_back_photo'],//经营者身份证反面
                'legal_hand_hold_idcard_photo' => $merchant['legal_hand_hold_idcard_photo'],//经营者手持身份证照
                'legal_idcard_begin_date' => $merchant['legal_idcard_begin_date'],//经营者身份证有效期
                'legal_idcard_end_date' => $merchant['legal_idcard_end_date'],//经营者身份证有效期
                'legal_idcard_validity_type' => $merchant['legal_idcard_validity_type'],//经营者身份证是否长期有效，0短期，1长期
            ],
            // 营业执照信息
            'license_info' => [
                'license_code' => $merchant['license_code'],//营业执照信用代码
                'license_name' => $merchant['license_name'],//营业执照公司名
                'license_address' => $merchant['license_address'],//营业执照注册地址
                'license_photo' => $merchant['license_photo'],//营业执照照片
                'license_begin_date' => $merchant['license_begin_date'],//营业执照有效期
                'license_end_date' => $merchant['license_end_date'],//营业执照有效期
                'license_validity_type' => $merchant['license_validity_type'],//营业执照是否长期有效：0短期，1长期
            ],
            // 结算信息
            'account_info' => [
                'account_type' => $merchant['account_type'],//结算账户类型，1个人账户，2企业账户
                'settle_type' => $merchant['settle_type'],//结算人类型，1非法人结算，2法人结算，3对公结算
                'bank_card_photo' => $merchant['bank_card_photo'],//结算卡照片
                'opening_license_photo' => $merchant['opening_license_photo'],//开户许可证照片，企业账户对公结算必传
                'bank_card_no' => $merchant['bank_card_no'],//结算账户
                'bank_cell_phone' => $merchant['bank_cell_phone'],//银行预留手机号
                'bank_shot_name' => $merchant['bank_shot_name'],//银行简称
                'bank_full_name' => $merchant['bank_full_name'],//结算银行全称
                'bank_branch_name' => $merchant['bank_branch_name'],//结算银行支行名称
                'unionpay_code' => $merchant['unionpay_code'],//联行号
                'settle_name' => $merchant['settle_name'],//结算人姓名
                'account_province' => $merchant['account_province'],//开户行省
                'account_city' => $merchant['account_city'],//开户行市
                'account_area' => $merchant['account_area'],//开户行区县
                'account_area_str' => $merchant['account_area_str'],//开户行地区
                'settle_idcard_no' => $merchant['settle_idcard_no'],//结算人身份证号
                'settle_idcard_front_photo' => $merchant['settle_idcard_front_photo'],//结算人身份证正面
                'settle_idcard_back_photo' => $merchant['settle_idcard_back_photo'],//结算人身份证反面
                'settle_idcard_begin_date' => $merchant['settle_idcard_begin_date'],//结算人身份证有效期
                'settle_idcard_end_date' => $merchant['settle_idcard_end_date'],//结算人身份证有效期
                'settle_idcard_validity_type' => $merchant['settle_idcard_validity_type'],//结算人身份证是否长期有效，0短期，1长期
                'unincorporated_photo' => $merchant['unincorporated_photo'],//非法人结算授权书
            ],
            // 店铺地址信息
            'address_info' => [
                'province_code' => $merchant['province_code'],//商户所在省
                'city_code' => $merchant['city_code'],//商户所在市
                'area_code' => $merchant['area_code'],//商户所在区县
                'store_area_str' => $merchant['store_area_str'],//商户所在地区
                'street_address' => $merchant['street_address'],//商户所在街道地址
                'longitude' => $merchant['longitude'],//商户经度
                'latitude' => $merchant['latitude'],//商户纬度
            ],
            // 门店信息
            'shop_info' => [
                'store_logo' => $merchant['store_logo'],//商户Logo
                'store_front_photo' => $merchant['store_front_photo'],//门头照
                'store_cash_photo' => $merchant['store_cash_photo'],//收银台照
                'store_env_photo' => $merchant['store_env_photo'],//店内环境照
                'delivery_distance' => $merchant['delivery_distance'],//配送距离
                'business_hours_start' => $merchant['business_hours_start'],//开始营业时间
                'business_hours_end' => $merchant['business_hours_end'],//结束营业时间
                'business_status' => $merchant['business_status'],//经营状态，0休息中，1营业中
                'business_week' => $merchant['business_week'],//营业日
                'business_weeks' => explode(',', $merchant['business_week']),
            ],
        ];

        return json(['code' => 1, 'msg' => 'OK', 'data' => $result]);
    }

    /**
     * 店铺列表
     * @return \think\response\Json
     */
    function storeList()
    {
        $skip = input('skip', 0);
        $limit = input('limit', 10);

        $where = [];

        $where[] = ['merchant_id', '=', $this->user_id];

        $list = model\Store::where($where)->limit($skip, $limit)->order('create_time desc')->select();

        foreach ($list as &$item) {
            $item['store_logo'] = $item['store_logo'] ? replace_url($item['store_logo']) : replace_url('/static/images/logo.png');
            $item['status_str'] = CommonEnum::examineText($item['status']);
            $item['store_type_str'] = MerchantEnum::storeTypeText($item['user_type']);
        }

        $result = [
            'data' => $list,
            'is_more' => count($list) >= $limit,
        ];

        return json(["code" => 1, "msg" => "获取成功", "data" => $result]);
    }

}